package dicky.fortunetelling;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
import org.achartengine.renderer.XYSeriesRenderer;

import dicky.fortunetelling.R;
import dicky.fortunetelling.logic.BodyClockUtil;
import dicky.fortunetelling.logic.GenChartUtil;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

public class bodyclock extends Activity {
	private Button btn1;
	private EditText date1,date2;
	private Calendar cal = Calendar.getInstance();
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private LayoutInflater layoutInflater;
	private LinearLayout chartLayout;
	
	private String tag=bodyclock.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bodyclock);
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				bodyclock();
			}
		});
        
        date1=(EditText)findViewById(R.id.date1);
        date1.setText(BodyClockUtil.GetBirtyday(this));
        date1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DatePickerDialog dg = new DatePickerDialog(
						bodyclock.this, listener1, cal
								.get(Calendar.YEAR), cal.get(Calendar.MONTH),
						cal.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        
        date2=(EditText)findViewById(R.id.date2);
        date2.setText(df.format(new Date()));
        date2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					cal.setTime(df.parse(date2.getText().toString()));
				} catch (ParseException e) {
					cal.setTime(new Date());
				}
				DatePickerDialog dg = new DatePickerDialog(
						bodyclock.this, listener2, cal
								.get(Calendar.YEAR), cal.get(Calendar.MONTH),
						cal.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        
    }
	
	private DatePickerDialog.OnDateSetListener listener1 = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date1.setText(df.format(cal.getTime()));
		}
	};
	
	private DatePickerDialog.OnDateSetListener listener2 = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date2.setText(df.format(cal.getTime()));
		}
	};

	
	private void bodyclock(){
		try {
			BodyClockUtil.SetBirtyday(this,date1.getText().toString());
			List list=BodyClockUtil.GetBodyClock(date1.getText().toString(),
					date2.getText().toString(),-3,20);
			
			ShowChart(bodyclock.this,list);
		} catch (Exception e) {
			Log.d(tag, e.toString());
		}
//		Intent intent = null;
//		intent=new MultipleTemperatureChart().execute(bodyclock.this);
//		startActivity(intent);
	}
	
	public void ShowChart(Context ctx,List list) {
		String[] titles = new String[] { "智力", "情绪", "体力" };

		int[] colors = new int[] { Color.BLUE, Color.YELLOW,Color.RED };
		PointStyle[] styles = new PointStyle[] { PointStyle.POINT,
				PointStyle.POINT,PointStyle.POINT };
		XYMultipleSeriesRenderer renderer = GenChartUtil.buildRenderer(colors, styles, true);

		renderer.setChartTitle("人体生物钟");
		renderer.setChartTitleTextSize(22);
		renderer.setXTitle("");
		renderer.setLegendTextSize(22);
		renderer.setAxesColor(Color.BLACK);
		renderer.setLabelsTextSize(20);
		renderer.setAxisTitleTextSize(22);
		renderer.setXAxisMin(0);
		renderer.setXAxisMax(10);
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(100);
		renderer.setAxesColor(Color.BLACK);
		renderer.setApplyBackgroundColor(true);//是否可以自定义背景色
		renderer.setBackgroundColor(Color.WHITE); //chart内部的背景色
		renderer.setMarginsColor(Color.WHITE);//chart边缘部分的背景色
		renderer.setZoomLimits(new double[] { 0,23,0,100 });
		renderer.setPanLimits(new double[] { 0,23,0,100 });
		renderer.setPanEnabled(true, false);
		renderer.setZoomEnabled(true, false);
		renderer.setXLabels(0);
		renderer.setXLabelsAngle(-25);
		renderer.setYLabels(0);
		renderer.addYTextLabel(40, "低潮");
		renderer.addYTextLabel(60, "正常");
		renderer.addYTextLabel(100, "高潮");
		renderer.setMargins(new int[]{40,20,50,20});
        
		List x = new ArrayList();
		List y = new ArrayList();
		double[] i_x,i_y,m_x,m_y,p_x,p_y;
		i_x=new double[list.size()];
		i_y=new double[list.size()];
		m_x=new double[list.size()];
		m_y=new double[list.size()];
		p_x=new double[list.size()];
		p_y=new double[list.size()];
		HashMap map =null;
		String date="";
		for (int i=0;i<list.size();i++){
			map=(HashMap)list.get(i);
			i_x[i]=i;
			m_x[i]=i;
			p_x[i]=i;
			i_y[i]=Double.parseDouble(map.get("i_value")+"");
			m_y[i]=Double.parseDouble(map.get("m_value")+"");
			p_y[i]=Double.parseDouble(map.get("p_value")+"");
			date=map.get("date")+"";
			date=date.substring(5, date.length());
			renderer.addTextLabel(i, date);
			
			Log.v(tag, map.get("date")+",i_y="+i_y[i]+",m_y="+m_y[i]+",p_y="+p_y[i]);
		}
		
		x.add(i_x);
		x.add(m_x);
		x.add(p_x);

		y.add(i_y);
		y.add(m_y);
		y.add(p_y);

		XYMultipleSeriesDataset dataset = GenChartUtil.buildDataset(titles, x, y);

		
		
//		GenChartUtil.setChartSettings(renderer, "人体生物钟", "日期", "", -1, 12, 0, 100,
//				Color.WHITE, Color.WHITE,Color.WHITE);

		View chart = ChartFactory.getLineChartView(this, dataset, renderer);
		layoutInflater = getLayoutInflater();
		chartLayout = (LinearLayout)findViewById(R.id.chartview);
		chartLayout.removeAllViews();
		chartLayout.addView(chart);
	}
}
